/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity;

import db.DBContext;
import db.PGKeys;
import db.RedisKey;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONValue;
import share.PGHelper;
import share.PGMacro;

/**
 *
 * @author Salm
 */
public class UrgentQuest implements PGEntity {
    private final String uid;
    
    private State state;
    private long next, deadLine;
    private Map<String, Object> totalPrize;
    
    private final Map<String, String> newValues = new HashMap();

    private UrgentQuest(String uid) {
        this.uid = uid;
    }
    
    public static UrgentQuest getUQ(String uid){
        UrgentQuest uq = new UrgentQuest(uid);
        uq.updateFromDB();
        
        return uq;
    }
    
    public static RedisKey key(String uid)
    {
        return User.redisKey(uid).getChild(PGKeys.FD_URGENT_QUEST);
    }
    
    public static void destroy(String uid)
    {
        DBContext.Redis().del(key(uid));
    }

    @Override
    public void updateFromDB() {
        Map<String, String> data = DBContext.Redis().hgetall(key(uid));
        newValues.clear();
        this.fillFromData(data);
    }
    
    private void fillFromData(Map<String, String> data)
    {
        this.state = State.parse(data.get(PGMacro.UQ_STATE));
        this.next = PGHelper.toLong(data.get(PGMacro.UQ_NEXT_TIME));
        this.deadLine = PGHelper.toLong(data.get(PGMacro.UQ_DEADLINE));
        
        String ttPzJson = data.get(PGMacro.UQ_TOTAL_PRIZE);
        this.totalPrize = (Map) ((PGHelper.isNullOrEmpty(ttPzJson))?null:
                (JSONValue.parse(ttPzJson)));
    }

    @Override
    public void saveToDB() {
        if (!newValues.isEmpty())
        {
            DBContext.Redis().hset(key(uid), newValues);
            newValues.clear();
        }
    }

    public String getUid() {
        return uid;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
        newValues.put(PGMacro.UQ_STATE, String.valueOf(state));
    }

    public long getNext() {
        return next;
    }

    public void setNext(long next) {
        this.next = next;
        newValues.put(PGMacro.UQ_NEXT_TIME, String.valueOf(next));
    }

    public long getDeadLine() {
        return deadLine;
    }

    public void setDeadLine(long deadLine) {
        this.deadLine = deadLine;
        newValues.put(PGMacro.UQ_DEADLINE, String.valueOf(deadLine));
    }

    public Map<String, Object> getTotalPrize() {
        return totalPrize;
    }

    public void setTotalPrize(Map<String, Object> totalPrize) {
        this.totalPrize = totalPrize;
        newValues.put(PGMacro.UQ_TOTAL_PRIZE, JSONValue.toJSONString(totalPrize));
    }
    
    public Object dump()
    {
        return DBContext.Redis().hgetall(key(uid));
    }
    
    public static enum State
    {
        LOCKED,
        COOLDOWN,
        HAVE_QUEST;
        
        public static State parse(String s)
        {
            try
            {
                return valueOf(s);
            }
            catch (Exception ex)
            {
                return LOCKED;
            }
        }
    }
}